import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {Router} from '@angular/router';
import {AppService} from '../../service/app/app.service';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
  registerForm: FormGroup;
  constructor(private builder: FormBuilder,
              private appService: AppService,
              private router: Router) {
    this.registerForm = this.builder.group({
      email: ['', ],
      user_name: ['', ],
      passwd: ['', ],
      code: [''],
    });
  }

  ngOnInit() {
  }
  enterLogin() {
    this.router.navigate(['login']).catch();
  }
  registe() {
    this.appService.post(this.appService.urls.register, this.registerForm.value).subscribe((resp: any) => {
      const result = resp.body;
      if (result.ret === 1) {
        localStorage.setItem('email', this.registerForm.value.email);
        localStorage.setItem('passwd', this.registerForm.value.passwd);
        localStorage.setItem('token', result.data.token);
        localStorage.setItem('user_id', result.data.user_id);
        localStorage.setItem('status', 'off');
        this.router.navigate(['home']).catch();
      }
    });
  }
}
